<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- <h2>组织结构</h2> -->
      <!-- 头部 -->
      <el-card class="tree-card">
        <!-- 结构内容 -->
        <!-- is-root="true" 是根节点 -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts"/>
        <!-- 放置一个el-tree -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="{data}" 接收传递给插槽的数据   data 每个节点的数据对象-->
          <template #default="{data}">
            <tree-tools
              :tree-node="data"
              @delDepts="getDepartments"
              @addDepts="addDepts"
              @editDepts="editDepts"
            />
          </template>
        </el-tree>
      </el-card>
    </div>
    <add-dept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @addDepts="getDepartments"
    />
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import TreeTools from './components/tree-tools'
import { transListToTreeDate } from '@/utils'
import AddDept from './components/add-dept'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: {},
      departs: [],
      defaultProps: {
        label: 'name'
      },
      showDialog: false,
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = transListToTreeDate(result.depts, '')
    },
    // 添加部门弹出框
    addDepts(node) {
      this.showDialog = true
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true // 显示弹层
      this.node = node // node点击编辑的部门
      this.$refs.addDept.getDeparDetail(node.id)
    }
  }
}
</script>

<style  scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
